/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.spring.boot;

import com.jakewharton.nopen.annotation.Open;

import io.sentry.SentryOptions;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.event.Level;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Configuration for Sentry integration.
 *
 * @since 2021-07-29
 */
@ConfigurationProperties("sentry")
@Open
public class SentryProperties extends SentryOptions {
    /**
     * Weather to use Git commit id as a release.
     */
    private boolean useGitCommitIdAsRelease = true;

    /**
     * Turns tracing on or off. Default is disabled.
     *
     * @deprecated use {{@link SentryProperties#setTracesSampleRate(Double)} } or create {@link
     * TracesSamplerCallback} bean instead
     */
    @Deprecated
    private boolean enableTracing;

    /**
     * Report all or only uncaught web exceptions.
     */
    private int exceptionResolverOrder = 1;

    /**
     * Defines the {@link io.sentry.spring.SentryUserFilter} order. The default value is {@link
     * org.springframework.core.Ordered.LOWEST_PRECEDENCE}, if Spring Security is auto-configured, its
     * guaranteed to run after Spring Security filter chain.
     */
    private @Nullable Integer userFilterOrder;

    /**
     * Logging framework integration properties.
     */
    private @NotNull Logging logging = new Logging();

    public boolean isUseGitCommitIdAsRelease() {
        return useGitCommitIdAsRelease;
    }

    public void setUseGitCommitIdAsRelease(boolean useGitCommitIdAsRelease) {
        this.useGitCommitIdAsRelease = useGitCommitIdAsRelease;
    }

    @Deprecated
    public boolean isEnableTracing() {
        return enableTracing;
    }

    @Deprecated
    public void setEnableTracing(boolean enableTracing) {
        this.enableTracing = enableTracing;
    }

    /**
     * Returns the order used for Spring SentryExceptionResolver, which determines whether all web
     * exceptions are reported, or only uncaught exceptions.
     *
     * @return order to use for Spring SentryExceptionResolver
     */
    public int getExceptionResolverOrder() {
        return exceptionResolverOrder;
    }

    /**
     * Sets the order to use for Spring SentryExceptionResolver, which determines whether all web
     * exceptions are reported, or only uncaught exceptions.
     *
     * @param exceptionResolverOrder order to use for Spring SentryExceptionResolver
     */
    public void setExceptionResolverOrder(int exceptionResolverOrder) {
        this.exceptionResolverOrder = exceptionResolverOrder;
    }

    /**
     * get User Filter Order
     *
     * @return filter order
     */
    public @Nullable Integer getUserFilterOrder() {
        return userFilterOrder;
    }

    /**
     * set User Filter Order
     *
     * @param userFilterOrder order
     */
    public void setUserFilterOrder(final @Nullable Integer userFilterOrder) {
        this.userFilterOrder = userFilterOrder;
    }

    /**
     * get Logging
     *
     * @return Logging
     */
    public @NotNull Logging getLogging() {
        return logging;
    }

    /**
     * set Logging
     *
     * @param logging for set
     */
    public void setLogging(@NotNull Logging logging) {
        this.logging = logging;
    }

    /**
     * Logging
     *
     * @since 2021-07-29
     */
    @Open
    public static class Logging {
        /**
         * Enable/Disable logging auto-configuration.
         */
        private boolean enabled = true;

        /**
         * Minimum logging level for recording breadcrumb.
         */
        private @Nullable Level minimumBreadcrumbLevel;

        /**
         * Minimum logging level for recording event.
         */
        private @Nullable Level minimumEventLevel;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public @Nullable Level getMinimumBreadcrumbLevel() {
            return minimumBreadcrumbLevel;
        }

        public void setMinimumBreadcrumbLevel(@Nullable Level minimumBreadcrumbLevel) {
            this.minimumBreadcrumbLevel = minimumBreadcrumbLevel;
        }

        /**
         * getMinimumEventLevel
         *
         * @return Level
         */
        public @Nullable Level getMinimumEventLevel() {
            return minimumEventLevel;
        }

        public void setMinimumEventLevel(@Nullable Level minimumEventLevel) {
            this.minimumEventLevel = minimumEventLevel;
        }
    }
}
